<?php if(!defined('FRAME')) exit('No direct script access allowed');
/**
 * 日志
 * @author wisoch@hotmail.com
 *
 */
class Log {
	/**
	 * 写日志
	 * 格式: 时间(2012/01/09 20:08:20)\t message(出错信息)\t IP(服务器IP)  \t clientIP(客户端IP) \t 客户端信息 
	 * @param string $alias
	 * @param string $level
	 * @param boolean $isMail
	 * @param boolean $isPhone
	 */
	public static function write($message, $level = 'ERROR', $isMail = true, $isPhone = false) {
		$config = Config::Instance();
		
		$time = date('Y/m/d H:i:s', time());
		$server_ip = isset($_SERVER['SERVER_ADDR'])? $_SERVER['SERVER_ADDR']: '';
		$client_ip = Func::clientIp();
		$client_info = isset($_SERVER['HTTP_USER_AGENT'])? $_SERVER['HTTP_USER_AGENT']: '';
		
		$content[] = $time;
		$content[] = $message;
		$content[] = $server_ip;
		$content[] = $client_ip;
		$content[] = $client_info;
		
		$content[] = "\r\n";
		$content = implode("\t", $content);
		
		$dir = trim($config->get('runtime.monitor.dir'), '/\\').'/';
		if(!file_exists($dir)) {
			FileSystem::mkdir($dir);
		}
		$email = $config->get('runtime.monitor.email');
		$phone = $config->get('runtime.monitor.phone');
		
		//写日志
		$folder = date('Y-m-d', time());
		if(!file_exists($dir . $folder)) {
			@mkdir($dir . $folder, 0777);
		}
		$error = 'error.txt';
		$handle = fopen($dir . $folder . '/' . $error, 'a+');
		if($handle) {
			@fwrite($handle, $content);
	    	@fclose($handle);
		}
	}
}